<?php 
namespace Home\Controller;
use Hdphp\Library\Controller;

/**
* 
*/
class CommonController extends Controller
{
	//顶级导航,左边栏
	protected function topNav(){
		//读取顶级分类
		$sql="SELECT * FROM hd_category WHERE pid = 0 ";
		$typeData=M()->query($sql);

		//读取左边栏数据
		foreach ($typeData as $key => $value) {
			$typeData[$key]['son']=M()->query("SELECT * FROM hd_category WHERE pid= {$value['cid']}");
		}
		$this->assign('typeData',$typeData);

		//提问总数
		$sql="SELECT COUNT(*) as total FROM hd_ask";
		$totalAsk=M()->query($sql);
		$this->assign('totalAsk',$totalAsk[0]);

	}


	//面包屑导航
	protected function Breadcrumbs($cid){
		//处理面包屑导航
		$cateData =M()->query("SELECT * FROM hd_category");
		$parentsCate = $this->_getParents($cateData,$cid);
		//数组反转
		$parentsCate = array_reverse($parentsCate);
		$this->assign('parentsCate',$parentsCate);
	}


	//通过子类id找父级
	private function _getParents($data,$cid){
		$temp=array();
		foreach ($data as $key => $value) {
			if($value['cid']==$cid){
				$temp[]=$value;
				$temp=array_merge($temp,$this->_getParents($data,$value['pid']));
			}
		}
		return $temp;
	}

	//问答右边统计信息
	protected function rightInfo(){
		//用户资料统计
		if(isset($_SESSION['uid'])){
			$sql="SELECT * FROM hd_user WHERE uid={$_SESSION['uid']}";
			$userinfo=M()->query($sql);
			$this->assign('userinfo',$userinfo[0]);
			// p($userinfo);die;
		}

		//当日回答问题最多的人
		$dayTime=strtotime(date('Y-m-d'));
		$sql="SELECT *,count(a.uid) as max,a.accept as ac,u.accept as uc FROM hd_answer as a JOIN hd_user as u ON a.uid=u.uid WHERE time>={$dayTime} GROUP BY a.uid ORDER BY max DESC LIMIT 1";
		$maxAnswerDay=M()->query($sql);
		$this->assign('maxAnswerDay',$maxAnswerDay);
// p($maxAnswerDay);die;
		//历史回答问题最多的人
		$sql="SELECT * FROM hd_user WHERE answer=(SELECT MAX(answer) FROM hd_user)";
		$maxAnswer=M()->query($sql);
		$this->assign('maxAnswer',$maxAnswer);
		//采纳率
		// $res=round($maxAnswer[0]['accept']/$maxAnswer[0]['answer']*100,2);
// p($maxAnswerDay);die;
		//助人光荣榜

		$sql="SELECT * FROM hd_user ORDER BY accept DESC LIMIT 3";
		$helpData=M()->query($sql);
		$this->assign('helpData',$helpData);

	}

/*
//当日最多
    [0] => Array
        (
            [anid] => 2
            [content] => www.houdunwang.com
            [time] => 1439358400
            [accept] => 0
            [uid] => 2
            [asid] => 1
            [max] => 3
        )


//历史最多
    [0] => Array
        (
            [uid] => 2
            [username] => houdunwang
            [passwd] => 7fef6171469e80d32c0559f88b377245
            [ask] => 2
            [answer] => 30
            [accept] => 9
            [point] => 145
            [exp] => 127
            [restime] => 1377048366
            [logintime] => 1378115529
            [loginip] => 124.205.157.210
            [lock] => 0
            [face] => 
        )

*/

}





 ?>